package w20rest.java;

import org.apache.chemistry.opencmis.client.api.*;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CVUTISAllfrescoTest {


    @Test
    public void test() throws IOException {
        SessionFactory factory = SessionFactoryImpl.newInstance();
        Map<String, String> parameter = new HashMap<String, String>();

//        parameter.put(SessionParameter.USER, "admin");
//        parameter.put(SessionParameter.PASSWORD, "Alf1987");
//        parameter.put(SessionParameter.ATOMPUB_URL, "https://alfresco.is-dev.fit.cvut.cz/alfresco/cmisatom/");


//        parameter.put(SessionParameter.USER, "admin");
//        parameter.put(SessionParameter.PASSWORD, "admin");
//        parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:8642/alfresco/service/cmis");


        parameter.put(SessionParameter.USER, "fiserale");
        parameter.put(SessionParameter.ATOMPUB_URL, "https://alfresco.fit.cvut.cz/alfresco/cmisatom");

        parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        parameter.put(SessionParameter.REPOSITORY_ID, "Main Repository");


        printRepositories(factory, parameter);

        Session session = factory.createSession(parameter);
        printRootFolderContent(session);
    }

    private void printRepositories(SessionFactory factory, Map<String, String> parameters) {
        final List<Repository> repositories = factory.getRepositories(parameters);
        for (Repository repository : repositories) {
            System.out.println(repository.getId() + "  name= " + repository.getName() + " /");
        }
    }

    private void printRootFolderContent(Session session) {
        Folder root = session.getRootFolder();
        ItemIterable<CmisObject> children = root.getChildren();
        for (CmisObject o : children) {
            System.out.println(o.getName());
        }
    }


}
